package com.ryan;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;

import org.apache.commons.lang3.StringUtils;

public class JarUtil {

	
	private static void buildFile(JarFile sourceJar, List<String[]> lst, JarOutputStream jos) throws Exception{
		byte[] buffer = new byte[1024 * 4];
		JarEntry jarEntry = null;
		String strPath = null;
		for(String[] arrs : lst){
			if(arrs.length != 2)
				continue;
			jarEntry = new JarEntry(arrs[0]);
			strPath = arrs[1];
			if(!StringUtils.isEmpty(strPath)){
				InputStream data = null;
				if(arrs[0].equals(strPath)){
					jos.putNextEntry(jarEntry);
					data = sourceJar.getInputStream(jarEntry);
				}else if(new File(strPath).exists()){
					jos.putNextEntry(jarEntry);
					data = new FileInputStream(strPath);
				}else{
					continue;
				}
				int num = 0;
				while((num = data.read(buffer)) > 0){
					jos.write(buffer, 0 , num);
				}
				jos.flush();
			}
		}
	}
	
	
	public static void replaceFile(String sourceJar, String targetJar, Map<String, String> fileReplaceMap){
		JarFile jar = null;
		JarOutputStream outputJar = null;
		try{
			jar = new JarFile(sourceJar);
			outputJar = new JarOutputStream(new FileOutputStream(targetJar));
			List<String[]> lst = new LinkedList<String[]>();
			JarEntry jarEntry = null;
			String[] arrs = null;
			boolean bExist = false;
			for(Enumeration<JarEntry> entity = jar.entries();entity.hasMoreElements();){
				jarEntry = entity.nextElement();
				bExist = false;
				if(fileReplaceMap != null){
					for(String key : fileReplaceMap.keySet()){
						if(jarEntry.getName().equals(key)){
							arrs = new String[2];
							arrs[0] = jarEntry.getName();
							arrs[1] = fileReplaceMap.get(key);
							lst.add(arrs);
							bExist = true;
						}
					}
					if(!bExist)
						lst.add(new String[]{jarEntry.getName(),jarEntry.getName()});
				}else{
					lst.add(new String[]{jarEntry.getName(),jarEntry.getName()});
				}
			}
			buildFile(jar, lst, outputJar);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			closeJar(jar);
			closeIO(outputJar);
		}
	}
	
	public static void closeJar(JarFile jar){
		if(jar != null){
			try {
				jar.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void closeIO(Closeable io){
		if(io!=null){
			try{
				io.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		String file1 = "D:/android/20131227/Multimode_Unipay_base.jar";
		String file2 = "D:/android/20131227/Multimode_Unipay_base_new.jar";
		Map<String,String> map = new HashMap<String, String>();
		map.put("assets/armeabi/libdecrypt.jar", "");
		map.put("assets/armeabi-v7a/libdecrypt.jar", "D:/android/20131227/UniPay计费SDK单机版(SV120)/中国联通沃商店UniPay计费SDK开发者帮助文档-单机版(SV1.2.0).doc");
		JarUtil.replaceFile(file1, file2, map);
		
		System.out.println(new File(file1).getParent());
	}
}
